﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmEditSchedule : Form
    {
        Form opener;
        DateTime weekStart, weekEnd;
        GeneralClasses.Schedule schedule;

        public frmEditSchedule(Form Opener)
        {
            weekStart = DateTime.Now.Date;

            switch (weekStart.DayOfWeek)
            {
                case System.DayOfWeek.Sunday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Sunday));
                    break;
                case System.DayOfWeek.Monday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Monday));
                    break;
                case System.DayOfWeek.Tuesday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Tuesday));
                    break;
                case System.DayOfWeek.Wednesday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Wednesday));
                    break;
                case System.DayOfWeek.Thursday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Thursday));
                    break;
                case System.DayOfWeek.Friday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Friday));
                    break;
                case System.DayOfWeek.Saturday:
                    weekStart = weekStart.AddDays(-(int)(GeneralClasses.DayOfWeek.DOW.Saturday));
                    break;
                default:
                    break;
            }

            opener = Opener;
            InitializeComponent();
        }

        void refresh()
        {
            weekEnd = weekStart.AddDays(GeneralClasses.DayOfWeek.numDays);

            lblStartEnd.Text = weekStart.Date.ToLongDateString() + " - " + weekEnd.Date.ToLongDateString();

            List<Person> people = Person.loadAllActiveNurses();

            clmEmployee.Items.Clear();
            foreach (Person item in people)
            {
                clmEmployee.Items.Add(item.FullName);
            }

            schedule = new GeneralClasses.Schedule(weekStart, weekEnd);

            dgvSchedule.Rows.Clear();
            foreach (GeneralClasses.Day item in schedule.Days)
            {
                foreach (GeneralClasses.Shift current in item.getAllShifts())
                {
                    string skills = "";
                    foreach (Skill skill in current.Skills)
                    {
                        skills += skill.Name = " ";
                    }

                    /*if (current.Departments.Count > 0)
                    {
                        dgvSchedule.Rows.Add(current.getAssignedPersons()[0].FullName,
                            current.Name, current.StartTime.ToString(), current.EndTime.ToString(),
                            1, current.Departments[0].Name, skills);
                    }
                    else
                    {
                        dgvSchedule.Rows.Add(current.getAssignedPersons()[0].FullName,
                            current.Name, current.StartTime.ToString(), current.EndTime.ToString(),
                            1, "", skills);
                    }*/
                }
            }
        }

        private void btnPreviousWeek_Click(object sender, EventArgs e)
        {
            weekStart = weekStart.AddDays(-GeneralClasses.DayOfWeek.numDays);
            refresh();
        }

        private void btnNextWeek_Click(object sender, EventArgs e)
        {
            weekStart = weekStart.AddDays(GeneralClasses.DayOfWeek.numDays);
            refresh();
        }

        private void btnSaveCurrent_Click(object sender, EventArgs e)
        {
            //set employees in schedule to employees in dgvSchedule
            bool saved = true;

            if (saved)
            {
                MessageBox.Show("Saved changes to shown week's schedule.");
                refresh();
            }
            else
            {
                MessageBox.Show("Could not save changes to shown week's schedule.");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEditSchedule_FormClosing(object sender, FormClosingEventArgs e)
        {
            opener.Show();
        }

        private void frmEditSchedule_Shown(object sender, EventArgs e)
        {
            refresh();
        }
    }
}
